Skip to content

run-command(win32): call the shell "sh" again - #6563

Closed
sanford wants to merge 1 commit into
git-for-windows:mainfrom
sanford:busybox-sh-argv0
Closed

sanford wants to merge 1 commit into
git-for-windows:mainfrom
sanford:busybox-sh-argv0

Conversation

@sanford

@sanford sanford commented Nov 19, 2026

Copy link
Copy Markdown

Fixes #5184.

In the BusyBox flavor of MinGit, every command git runs through the shell fails with -c: applet not found. That covers ! aliases with anything more than a plain command, and editors, pagers and other configured commands that have arguments, like core.editor="sed -i s/^/x/". Hooks aren't affected: they're started through their #! line, which already works.

The cause is 92fe7c7 (run-command(win32): resolve the path to the Unix shell early), which went into 2.46. Since then prepare_shell_cmd() puts the full path that git_shell_path() finds into argv[0], where it used to put plain sh. In MinGit-busybox that path is busybox.exe, which path_lookup() finds through its applet fallback. BusyBox picks its applet from argv[0], and busybox isn't an applet, so it reads -c as the applet's name.

This patch puts sh in argv[0] again, on Windows only. mingw_spawnvpe() still looks up the same sh that git_shell_path() reports, and is_msys2_sh() already handles a plain sh, so the arguments are quoted as before. git var GIT_SHELL_PATH still shows the full path.

About the alternatives:

  • Shipping a copy of busybox.exe as sh.exe (build-extra#746) was declined because of its size.
  • core.shell (Allow specifying at runtime which POSIX shell to use #3301) is still the better long-term knob, and this patch doesn't get in its way. On its own, though, it doesn't fix this bug. With core.shell set to the path of busybox.exe, argv[0] is still busybox.exe. With core.shell set to the full path of MinGit's own ash.exe (a 42 kB launcher), it should work, but only once MinGit ships that config. I checked that ash.exe -c '…' works, and that a copy of ash.exe named sh.exe fixes this bug too. That copy would be a packaging-only fix at 42 kB, not the 844 kB of build-extra#746. As far as I can read the code, a bare name like ash would break hooks: the sh special case in path_lookup() returns the name without resolving it, and the #!/bin/sh line is then started as the program ash.

How I tested it: I used MinGit-2.56.1-busybox-mingw64, with git.exe replaced by builds of v2.56.1.windows.1 with and without this patch. The PATH held only MinGit's mingw64\bin and Windows' own folders, and everything ran inside an AppContainer.

shipped git.exe built without the patch built with the patch
git -c "alias.t=!echo one; echo two" t -c: applet not found, exit 127 same prints one, two, exit 0
git -c "core.editor=sed -i s/^/edited/" commit --allow-empty -c: applet not found, "there was a problem with the editor" same the editor runs, commit made
post-commit hook (#!/bin/sh), also from a core.hooksPath with a space in it runs runs runs

I didn't run Git's test suite on Windows.

This matters most inside Windows' AppContainer sandbox. MSYS2's sh.exe can't start there (microsoft/mxc#1061), and BusyBox-w32 runs fine, so MinGit-busybox is the only Git that can run hooks and aliases in such a sandbox.

Since 92fe7c7 (run-command(win32): resolve the path to the Unix
shell early, 2024-07-13), `prepare_shell_cmd()` puts the full path that
`git_shell_path()` finds for `sh` into `argv[0]`, where it used to put
plain "sh" and leave the lookup to `mingw_spawnvpe()`.

The two lookups find the same program, but in the BusyBox flavor of
MinGit that program is `busybox.exe`, found by the applet fallback in
`path_lookup()`.  BusyBox picks the applet to run from `argv[0]`, and
`busybox.exe` isn't one, so it takes the next argument as the applet's
name, and every command that goes through the shell fails:

	$ git -c alias.t='!echo one; echo two' t
	-c: applet not found

That covers `!` aliases with more than a plain command, and editors,
pagers and other configured commands given with arguments
(`core.editor="sed -i s/^/x/"` fails the same way).  Hooks aren't
affected.

Put "sh" in `argv[0]` again on Windows.  `mingw_spawnvpe()` finds the
same `sh` as before, `is_msys2_sh()` already recognizes "sh" and quotes
the arguments the same way, and BusyBox now runs its `sh` applet.
`git var GIT_SHELL_PATH` still shows the full path.

This fixes git-for-windows#5184.

Signed-off-by: Sanford Lincoln <12786+sanford@users.noreply.github.com>
@dscho

dscho commented Nov 19, 2026

Copy link
Copy Markdown
Member

It indeed is, and I fear that this PR would make that long-term knob harder. For example, this PR distracts me during the already limited time I have to look into the BusyBox stuff, and #3301 gets less and less attention as a consequence.

I didn't run Git's test suite on Windows.

Okay, that's good to know. I will match that by stopping my review here ;-)

@sanford

sanford commented Nov 20, 2026

Copy link
Copy Markdown
Author

Fair point, and sorry for the distraction. I'll close this so #3301 keeps the attention. If it's useful, I can test #3301 with MinGit-busybox inside an AppContainer, which is where I hit this, and report back there.

@sanford sanford closed this Nov 20, 2026
@dscho

dscho commented Nov 20, 2026

Copy link
Copy Markdown
Member

@sanford thank you for understanding, and for taking my unfortunately quite harsh feedback with good grace! I apologize for my tone.

To be quite honest, I really don't like that I fail to find the time to get this MinGit stuff done. Maybe this can serve as an explanation (but not an excuse!) for my inconsiderate reply to you. For years, I am dreaming of having a special opt-in mode in Git for Windows to replace the Git Bash with a Git Ash that uses MinGit instead, and that makes everything a lot faster (if slightly incompatible with Bash and the other Unix tools that use the MSYS2 runtime). sigh

@sanford

sanford commented Nov 20, 2026

Copy link
Copy Markdown
Author

No need to apologise, it was a fair call, and I appreciate the explanation. For what it's worth, we're in the same boat: we run Git inside AppContainer sandboxes, where MSYS2's runtime can't start yet and MinGit-busybox is the only Git that can run hooks and aliases. So a "Git Ash" mode would be very welcome here. I'll put the #3301 testing I offered on the PR itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

! aliases not working in MinGit busybox builds

2 participants